<%--
  Created by IntelliJ IDEA.
  User: 武露斌
  Date: 2021/10/27
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="../../js/jquery-1.8.2.min.js"></script>

    <script src="../../js/fileupload.js"></script>
    <link rel="stylesheet" href="../../css/img.css">
    <link rel="stylesheet" href="../../bootstrap-4.6.0-dist/css/bootstrap.min.css">

    <script src="../../jquery-validate/jquery.validate.min.js"></script>
    <script src="../../jquery-validate/messages_zh.js"></script>
    <script src="../../jquery-validate/validate.js"></script>

</head>
<body>
<!--1：form 表单提交路径 及 提交方式 必须定义-->
<form action="add.do" method="post">
    小区名称：<input type="text" name="hname" class="required chcharacter"><br>
    单元楼：<input type="text" name="building"><br>
    门牌号：<input type="text" name="number"><br><br>
    实景图：
    <div class="photoUpLoad" >
    <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
    <img src="" alt="" class="photoEcho">
</div><br><br>
    状态：<select name="statu">
    <option value="">请选择状态</option>
    <option value="0">待租</option>
    <option value="1">出租</option>
</select><br>
    挂牌时间：<input type="text" name="gtime"><br>
    出租时间：<input type="text" name="ctime"><br>
    房屋类型：
    <select name="tid">
        <option value="">请选择类型</option>
    </select><br>


    <!--2：submit提交-->
    <input type="submit" value="添加"  class="btn btn-warning">
</form>

</body>
</html>
<script>

    $.ajax({
        url:"listt.do",
        type:"post",
        dataType:"json",
        success:function (data) {
            for (let i = 0; i <data.length ; i++) {
                $("[name='tid']").append('<option value="'+data[i].tid+'">'+data[i].tname+'</option>')
            }
        }
    })


    $("form").validate({
        submitHandler:function (){
            var formData = new FormData($("form")[0]);
                $.ajax({
                    url: "add.do",
                    data:formData,
                    type: "post",
                    dataType: "json",
                    success:function (data) {
                        if (data>0){
                            alert("添加成功")
                            location="list.do"
                        }else{
                            alert("添加失败")
                        }
                    },processData:false,contentType:false
                })
        }
    });




    // function add() {
    //     var formData = new FormData($("form")[0]);
    //     $.ajax({
    //         url: "add.do",
    //         data:formData,
    //         type: "post",
    //         dataType: "json",
    //         success:function (data) {
    //             if (data>0){
    //                 alert("添加成功")
    //                 location="list.do"
    //             }else{
    //                 alert("添加失败")
    //             }
    //         },processData:false,contentType:false
    //     })
    // }


</script>
















